const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            photo,
            dataForm:{},
            childDataForm: [],
            merchant:[],
            dish_category:[],
        };
    },
    mounted() {
        this.getCategory();
    },
    methods: {
        changeMerchant(val){
            if (this.action == 'Add') {
                this.dataForm.Merchant_SerialNumber = val[0];
                this.dataForm.Merchant_Business_Scope_SerialNumber = val[1];
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: 'dish_info',
                        Condition: `Merchant_Mapping_Blue_Plate_SerialNumber='${val[val.length - 1]}'`,
                    },
                    success: (data) => {
                        this.childDataForm = data;
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                });
            }
        },
        getData(){
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 'f_merchant_business_mapping_blue_plate1',
                    Condition: window.btoa(unescape(encodeURIComponent(this.nodeKey))),
                    ExtensionCondition: '',
                },
                success: (data) => {
                    if (data.length == 1) {
                        this.dataForm = data[0];
                    }    
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest)
                },
            })
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                method: "post",
                data: {
                    Data: 'dish_info',
                    Condition: `Merchant_Mapping_Blue_Plate_SerialNumber='${this.nodeKey}'`,
                },
                success: (data) => {
                    this.childDataForm = data;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest)
                },
            });
        },
        handleAdd(formName) {
            if (this.action === 'Add') {
                this[formName].push({
                    is_Featured:'0',
                    is_Spicy:'0',
                    is_Vegetarian:'0'
                })
            } else if (this.action === 'Modify') {
                let lJSONObject_Data_Options = new Object();
                lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));
                let ls_primary_key = lJSONObject_Data_Options.primary_key;
                let lJSONObject_Data = {
                    is_Featured:'0',
                    is_Spicy:'0',
                    is_Vegetarian:'0'
                };
                lJSONObject_Data[ls_primary_key] = this.nodeKey;
                this[formName].push(lJSONObject_Data);
            }
        },
        getCategory() {
            // 获取所有菜品类别数据（包括父级和子级）
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName:'dish_category',
                    NodeCode:''
                },
                success: (data) => {
                    this.dish_category = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest)
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'merchant_business_mapping_blue_plate',
                    Condition: "Row_Status='0'", // 只查询有效店铺
                },
                success: (data) => {//
                    // 构建树形结构数据
                    data.forEach(item =>{
                        item.id = item.Merchant_Mapping_Blue_Plate_SerialNumber;
                        item.name = item.Merchant_Alias
                    })
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                        method: "post",
                        data: {
                            SQLSerialNumber: 'f_merchant_business_scope',
                            Condition: '',
                            ExtensionCondition: window.btoa(unescape(encodeURIComponent("AND (merchant_category.Parent_SerialNumber ='MC0000000000011' OR merchant_category.Parent_SerialNumber ='MC0000000000013')"))),
                        },
                        success: (data1) => {
                            data1.forEach(item=>{
                                item.id = item.Merchant_Business_Scope_SerialNumber;
                                item.name = item.Merchant_Category;
                                item.children = data.filter(i=> i.Merchant_Business_Scope_SerialNumber == item.Merchant_Business_Scope_SerialNumber);
                            })
                            data1 = data1.filter(i=>i.children.length > 0);
                            $.ajax({
                                headers: requestHeaders(),
                                url: `${mh}/generalBusiness/generalQueryByTableName`,
                                type: "POST",
                                data: {
                                    Data: 'merchant',
                                    Condition: "Row_Status='0'", // 只查询有效店铺
                                },
                                success: (data2) => {//
                                    data2.forEach(item=>{
                                        item.id = item.Merchant_SerialNumber;
                                        item.name = item.Merchant_Name;
                                        item.children = data1.filter(i=> i.Merchant_SerialNumber == item.Merchant_SerialNumber);
                                    })
                                    data2 = data2.filter(i=>i.children.length > 0);
                                    this.merchant = data2;
                                },
                                complete: (XMLHttpRequest) => {
                                    requestPretreatment(XMLHttpRequest);
                                },
                                dataType: "json",
                            });
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                    })
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                this.childDataForm.forEach(item=>{
                    item.Merchant_SerialNumber = this.dataForm.Merchant_SerialNumber;
                    item.Merchant_Business_Scope_SerialNumber = this.dataForm.Merchant_Business_Scope_SerialNumber;
                    item.Merchant_Mapping_Blue_Plate_SerialNumber = this.dataForm.Merchant_Mapping_Blue_Plate_SerialNumber;
                })
                const {add, modify} = this.deal();
                let lJSONObject_DataSet = {
                    Modify:{
                        dish_info:modify
                    },
                    Add:{}
                }
                if (add != null) {
                    lJSONObject_DataSet['Add']['dish_info'] = add;
                }
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSaveByRequestBody",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status === 'success') {
                        window.history.go(-1);
                    }
                    this.loading = false;
                });
            } else {
                this.$message({
                    message: '存在未填写的必填项！',
                    type: 'warning'
                });
            }
        },
        deal() {    //将待提交的数据中添加和修改分离出来'
            let add = [], modify = [];
            for (let i = 0; i < this.childDataForm.length; i++) {
                const obj = this.childDataForm[i];
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    add.push(obj);
                } else {
                    modify.push(obj);
                }
            }
            if (add.length == 0) {
                return { add: null, modify };
            }
            return { add, modify };
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-cascader', ylCascader)
app.component('yl-text-editor', ylTextEditor);
app.component('yl-select', ylSelect)
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
